package manage.model;


import m.common.model.*;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelQueryUtil;
import m.common.model.util.QueryCondition;
import m.system.cache.FlushCache;
import m.system.util.ArrayUtil;
import m.system.util.ObjectUtil;
import m.system.util.StringUtil;

import java.util.Date;
import java.util.List;

@TableMeta(name="os_admin_login",description="管理员登录表")
public class AdminLogin extends StatusModel implements UserModel,SessionModel,FlushCache, DictionaryModel {
	private static final long serialVersionUID = 1L;


	@LinkTableMeta(name="admin_group_oid",table=AdminGroup.class,description="用户组")
	private AdminGroup adminGroup;
	@LinkTableMeta(name="org_group_oid",table=OrgGroupView.class,description="当前机构组")
	private OrgGroupView orgGroup;
	@LinkTableMeta(name="head_image_oid",table=ImageInfo.class,description="头像")
	private ImageInfo headImage;

	@FieldMeta(name="token",type=FieldType.STRING,length=100,description="token")
	private String token;
	@FieldMeta(name="realname",type=FieldType.STRING,length=20,notnull=true,description="真实姓名")
	private String realname;
	@FieldMeta(name="user_no",type=FieldType.STRING,length=50,description="用户编号")
	private String userNo;
	@FieldMeta(name="username",type=FieldType.STRING,length=20,notnull=true,description="登录帐号")
	private String username;
	@FieldMeta(name="password",type=FieldType.STRING,length=50,notnull=true,description="登录密码")
	private String password;
	@FieldMeta(name="create_date",type=FieldType.DATE,description="创建时间")
	private Date createDate;
	@FieldMeta(name="last_login_time",type=FieldType.DATE,description="最后登录时间")
	private Date lastLoginTime;
	@FieldMeta(name="last_login_ip",type=FieldType.STRING,length=20,description="最后登录IP")
	private String lastLoginIp;
	@FieldMeta(name="login_count",type=FieldType.INT,description="登陆次数")
	private Integer loginCount;

	public String getUserNo() {
		return userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public Date getCreateDate() {
		return createDate;
	}
	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}
	public Date getLastLoginTime() {
		return lastLoginTime;
	}
	public void setLastLoginTime(Date lastLoginTime) {
		this.lastLoginTime = lastLoginTime;
	}
	public String getLastLoginIp() {
		return lastLoginIp;
	}
	public void setLastLoginIp(String lastLoginIp) {
		this.lastLoginIp = lastLoginIp;
	}
	public AdminGroup getAdminGroup() {
		return adminGroup;
	}
	public void setAdminGroup(AdminGroup adminGroup) {
		this.adminGroup = adminGroup;
	}
	public Integer getLoginCount() {
		return loginCount;
	}
	public void setLoginCount(Integer loginCount) {
		this.loginCount = loginCount;
	}
	public ImageInfo getHeadImage() {
		return headImage;
	}
	public void setHeadImage(ImageInfo headImage) {
		this.headImage = headImage;
	}
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	public String getToken() {
		return token;
	}
	public void setToken(String token) {
		this.token = token;
	}
	public OrgGroupView getOrgGroup() {
		return orgGroup;
	}
	public void setOrgGroup(OrgGroupView orgGroup) {
		this.orgGroup = orgGroup;
	}
	public String getUserType() {
		return "管理员";
	}
	public AdminLogin getCacheModel(String key) throws Exception {
		AdminLogin admin=new AdminLogin();
		admin.setOid(key);
		admin=ModelQueryUtil.getModel(admin,1);
		if(ObjectUtil.notEmptyOid(admin.getOrgGroup())){
			admin.setOrgGroup(ModelQueryList.getModel(admin.getOrgGroup(),"*","org.*","dept.*"));
		}
		return admin;
	}

	@Override
	public String getDictValue() {
		return this.getOid();
	}

	@Override
	public String getDictName() {
		return this.realname;
	}

	@Override
	public String getDictParentOid() {
		return "";
	}

	@Override
	public String getDictStatus() {
		return this.getStatus().equals("0")?"Y":"N";
	}

	@Override
	public List<AdminLogin> getCacheList(String key) throws Exception {
		QueryCondition con=null;
		if(!StringUtil.isSpace(key)){
			String[] groups= ArrayUtil.toNoEmptyArray(StringUtil.noSpace(key).split(","));
			ModelQueryList inQueryList=ModelQueryList.instance(AdminGroupLink.class, new String[] {"admin.oid"}, null,
				QueryCondition.and(
					QueryCondition.or(
						QueryCondition.in("adminGroup.oid", groups),
						QueryCondition.in("adminGroup.num", groups),
						QueryCondition.in("adminGroup.parent.oid", groups),
						QueryCondition.in("adminGroup.parent.num", groups),
						QueryCondition.in("adminGroup.org.oid", groups),
						QueryCondition.in("adminGroup.org.num", groups)
					)
				)
			);
			con=QueryCondition.in("oid",inQueryList);
		}
		return ModelQueryList.getModelList(AdminLogin.class,new String[]{"oid","status","realname","username"},null,con);
	}
}
